﻿#region Using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxPager;
using DevExpress.Web.Data;
using VirtualShopEngineData;
using VirtualShopEngineLogic;
using VirtualShopEngineLogic.Helpers;
#endregion

public partial class ProvinciaEdit : System.Web.UI.Page, IProvinciaView
{
    #region "Declaration"
    private ProvinciaPresenter _presenter;
    private int _currentPage = 0;
    #endregion

    #region "Properties"
    public string Abreviacion
    {
        get { return ((DevExpress.Web.ASPxEditors.ASPxTextBox)grdAbm.FindEditFormTemplateControl("txtAbreviacion")).Text; }
        set { ((DevExpress.Web.ASPxEditors.ASPxTextBox)grdAbm.FindEditFormTemplateControl("txtAbreviacion")).Text = value; }
    }
    public string Nombre
    {
        get { return ((DevExpress.Web.ASPxEditors.ASPxTextBox)grdAbm.FindEditFormTemplateControl("txtNombre")).Text; }
        set { ((DevExpress.Web.ASPxEditors.ASPxTextBox)grdAbm.FindEditFormTemplateControl("txtNombre")).Text = value; }
    }
    public string Iso
    {
        get { return ((DevExpress.Web.ASPxEditors.ASPxTextBox)grdAbm.FindEditFormTemplateControl("txtIso")).Text; }
        set { ((DevExpress.Web.ASPxEditors.ASPxTextBox)grdAbm.FindEditFormTemplateControl("txtIso")).Text = value; }
    }
    #endregion
    
    #region "PageEvents"
    protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.RegisterClientScriptBlock(GetType(), "jSonHelper", "<script type='text/javascript' src='../../Scripts/jSonHelper.js'></script>");
        ClientScript.RegisterClientScriptBlock(GetType(), "abmjs", "<script type='text/javascript' src='../../Scripts/abms.js'></script>");
        _presenter = new ProvinciaPresenter(this, new Paginator(10));
        SetGrid();
        LoadPageOnGrid();
    }
    #endregion

    #region "ControlEvents"
    protected void pgrGrid_PageIndexChanged(object sender, EventArgs e)
    {
        ASPxPager pgrGrid = (ASPxPager)sender;
        _currentPage = pgrGrid.PageIndex + 1;
        LoadPageOnGrid();
    }
    protected void grdAbm_RowUpdated(object sender, ASPxDataUpdatedEventArgs e)
    {
        _presenter.Save();
        e.ExceptionHandled = true;
        
    }
    protected void grdAbm_AfterPerformCallback(object sender, ASPxGridViewAfterPerformCallbackEventArgs e)
    {
        GetPaginator().PageIndex = GetCurrentPageIndex();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        _presenter.Save();
    }
    #endregion

    #region "Procedures"
    private void SetGrid()
    {
        grdAbm.DataSourceForceStandardPaging = false;
        grdAbm.SettingsEditing.PopupEditFormHorizontalAlign = DevExpress.Web.ASPxClasses.PopupHorizontalAlign.WindowCenter;
        grdAbm.SettingsEditing.PopupEditFormVerticalAlign = DevExpress.Web.ASPxClasses.PopupVerticalAlign.WindowCenter;
        grdAbm.SettingsEditing.PopupEditFormModal = true;
        grdAbm.SettingsEditing.PopupEditFormHeight = 110;
        grdAbm.SettingsEditing.PopupEditFormWidth = 300;
        grdAbm.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
    }
    private void LoadPageOnGrid()
    {
        grdAbm.DataSource = _presenter.GetPage(GetCurrentPageNumber());
        grdAbm.DataBind();
        SetPaginator(GetCurrentPageNumber());
    }
    private void SetPaginator(int pageNumber)
    {
        GetPaginator().PageIndex = pageNumber - 1;
    }
    private int GetCurrentPageNumber()
    {
        if (_currentPage != 0)
        {
            return _currentPage;
        }
        _currentPage = GetPaginator().PageIndex + 1;
        return _currentPage;
    }
    private ASPxPager GetPaginator()
    {
        return (ASPxPager)grdAbm.FindPagerBarTemplateControl("pgrGrid", GridViewPagerBarPosition.Bottom);
    }
    private int GetCurrentPageIndex()
    {
        return GetCurrentPageNumber() - 1;
    }
    #endregion
    protected void grdAbm_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
    {
        Provincia prov = _presenter.LoadProvincia((int)e.Keys[0]);
        _presenter.SaveChanges(prov);
        grdAbm.CancelEdit();
        e.Cancel=true;
    }
    protected void grdAbm_RowInserting(object sender, ASPxDataInsertingEventArgs e)
    {
      _presenter.Save();
      grdAbm.CancelEdit();
      e.Cancel = true;
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        int i = 0;
        foreach (Provincia row in (List<Provincia>)grdAbm.DataSource)
        {
            if (grdAbm.Selection.IsRowSelected(i))
            {
                
            }
            i++;
        }
    }
}

    